package com.xuan.shop.quartz;

import com.xuan.shop.dao.IUserDao;
import com.xuan.shop.entity.User;
import com.xuan.shop.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

/**
 * 定时器定时查询数据库将数据放入redis中
 */
@Slf4j
public class updateUsername extends QuartzJobBean {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IUserDao dao;
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            if(!stringRedisTemplate.keys("usernames").isEmpty()){
                log.debug("当前缓存没有任何用户名");
                stringRedisTemplate.delete("usernames");
            }
            List<String> list=dao.findAllUserName();
            log.debug("从数据库中查询所有用户名"+list.size());
            if(!list.isEmpty()){
                String[] strings=list.toArray(new String[list.size()]);
                stringRedisTemplate.opsForSet().add("usernames",strings);
            }


    }
}
